package com.kuang.gulimall.cart.controller;


import com.kuang.gulimall.cart.service.CartService;
import com.kuang.gulimall.cart.vo.CartItemVo;
import com.kuang.gulimall.cart.vo.CartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

@Controller
public class CartController {


    @Autowired
    CartService cartService;

    /**远程功能，获取购物项
     *
     * @param
     * @retur
     *
     */
    @ResponseBody
    @GetMapping("/currentUserCartItems")
    public List<CartItemVo> getCurrentUserCartItems(){

        return cartService.getUserCartItems();
    }

    @GetMapping("/deleteItem")
    public String deleteItem(@RequestParam("deleteId") Long deleteId){

        cartService.deleteItem(deleteId);

        return "redirect:http://cart.gulimall.com/cart.html";
    }

    @GetMapping("/checkItem")
    public String checkItem(@RequestParam("skuId") Long skuId,
                            @RequestParam("check") Integer check){

        cartService.checkItem(skuId,check);

        return "redirect:http://cart.gulimall.com/cart.html";
    }

    @GetMapping("/countItem")
    public String countItem(@RequestParam("skuId") Long skuId,
                            @RequestParam("num") Integer num){

        cartService.countItem(skuId,num);

        return "redirect:http://cart.gulimall.com/cart.html";
    }

    /*
    * 去购物车页面，这里的业务逻辑，都在service里实现
    *
    *
    * */
    @GetMapping("/cart.html")
    public String cartListPage(Model model){

        //1，快速都得到用户信息，id，userkey
//        UserInfoTo userInfoTo = CartInterceptor.threadLocal.get();
//        System.out.println(userInfoTo);
        CartVo cart = cartService.getCart();

        model.addAttribute("cart",cart);
        return "cartList";
    }


    /**
     * 拆分两个功能，通过两个controller控制，一个专门用来添加购物车，一个专门用来展示，这样就避免了重复刷新，疯狂调用的问题
     * @param skuId
     * @param num
     * @param ra
     * @return
     */
    @GetMapping("/addToCart")
    public String addToCart(@RequestParam("skuId") Long skuId, @RequestParam("num") Integer num, RedirectAttributes ra){

        cartService.addToCart(skuId,num);
        //这里model中的数据会传递到下一层
        ra.addAttribute("skuId",skuId);
        //   /表示当前页，这里不用redirectattribute，因为那个只能用一次
        return "redirect:http://cart.gulimall.com/addToCartSuccess.html";
    }

    /**
     * @description:解决重复提交的问题,跳转到成功页
     * @author Xiangmou
     * @param skuId
     * @param model
     * @return java.lang.String
     * @date 2021/7/18 11:06
     */

    @GetMapping("/addToCartSuccess.html")
    public String addToCartSuccessPage(@RequestParam("skuId") Long skuId,Model model){
        //重定向到成功页面，再次查询购物车数据即可

        CartItemVo item = cartService.getCartItem(skuId);

        model.addAttribute("item",item);
        return "success";
    }
}
